<script setup>
import { ref, computed, watch, onMounted } from "vue";
import { useDataFormatter } from "@/stores";
import {
  getPostInfoService,
  getPostCategoryService,
  getPostByCategoryService,
  searchPostService,
} from "@/api/info";

const category = ref([]);
const search = ref("");
const categoryId = ref(0);

const {
  data: postInfo,
  formattedData: formattedPosts,
  loadData: loadPosts,
} = useDataFormatter();

// 加载分类数据
const loadCategories = async () => {
  try {
    const response = await getPostCategoryService();
    category.value = [{ id: 0, name: "全部分类" }, ...response.data];
  } catch (err) {
    console.error("加载分类失败:", err);
  }
};

// 根据分类筛选文章
const handleCategoryChange = (event) => {
  categoryId.value = Number(event.target.value);

  try {
    // 按分类加载文章
    if (categoryId.value === 0) {
      // 加载全部文章
      loadPosts(() => getPostInfoService(5));
    } else {
      // 加载指定分类文章
      loadPosts(() => getPostByCategoryService(categoryId.value));
    }
  } catch (err) {
    console.error("加载文章失败:", err);
  }
};

onMounted(() => {
  loadPosts(() => getPostInfoService(5)); // 加载文章数据
  loadCategories(); // 加载分类数据
});

// 搜索功能，监听search值变化
watch(search, async (newVal) => {
  if (newVal.trim() === "") {
    // 如果搜索框为空，重新加载当前分类的文章
    if (categoryId.value === 0) {
      await loadPosts(() => getPostInfoService(5));
    } else {
      await loadPosts(() => getPostByCategoryService(categoryId.value));
    }
    return;
  }

  // 执行搜索
  try {
    await loadPosts(() => searchPostService(newVal));
  } catch (err) {
    console.error("搜索失败:", err);
  }
});

// 修改搜索按钮的点击事件
const handleSearch = () => {
  // 直接触发 watch 的响应
  search.value = search.value.trim();
};
</script>

<template>
  <!-- 主要内容 -->
  <div class="container main-content">
    <!-- 搜索和分类 -->
    <div class="search-card">
      <div class="search-content">
        <!-- 搜索框 -->
        <div class="search-box">
          <div class="relative">
            <input
              type="text"
              class="search-input"
              placeholder="搜索攻略..."
              v-model="search"
              @keyup.enter="handleSearch"
            />
            <button class="search-button">
              <img src="/public/搜索.svg" alt="搜索" class="search-icon" />
            </button>
          </div>
        </div>
        <!-- 分类选择 -->
        <div class="filter-group">
          <select
            class="filter-select"
            v-model="categoryId"
            @change="handleCategoryChange"
          >
            <option v-for="(item, index) in category" :key="item.id" :value="item.id">
              {{ item.name }}
            </option>
          </select>
          
          <select class="filter-select">
            <option>最新发布</option>
            <option>最多浏览</option>
            <option>最多收藏</option>
            <option>最多评论</option>
          </select>
        </div>
      </div>
    </div>

    <!-- 热门标签 -->
    <div class="tags-card">
      <h3 class="tags-title">热门标签</h3>
      <div class="tags-list">
        <a href="#" class="tag">新手攻略</a>
        <a href="#" class="tag">农场布局</a>
        <a href="#" class="tag">矿洞技巧</a>
        <a href="#" class="tag">社交攻略</a>
        <a href="#" class="tag">节日活动</a>
        <a href="#" class="tag">赚钱技巧</a>
      </div>
    </div>

    <!-- 攻略列表 -->
    <div class="guide-list">
      <!-- 当没有文章时显示提示 -->
      <div v-if="formattedPosts.length === 0" class="empty-tips">
        <img src="/public/empty.svg" alt="空状态" class="empty-icon" />
        <p>当前分类下还没有攻略，快去发布第一篇吧！</p>
      </div>
      <!-- 攻略卡片 1 -->
      <div class="guide-card" v-for="(item, index) in formattedPosts" :key="item.id">
        <div class="guide-content">
          <div class="guide-header">
            <div class="guide-author">
              <img class="author-avatar" :src="item.user.avatar_url" alt="作者头像" />
              <div class="author-info">
                <p class="author-name">{{ item.user.username }}</p>
                <div class="author-meta">
                  <time :datetime="item.formattedDate">{{ item.formattedDate }}</time>
                  <span>•</span>
                  <span v-if="item.category_id == 1">新手指导</span>
                  <span v-else-if="item.category_id == 2">农场经营</span>
                  <span v-else-if="item.category_id == 3">矿洞探险</span>
                  <span v-else-if="item.category_id == 4">社交攻略</span>
                  <span v-else-if="item.category_id == 5">钓鱼技巧</span>
                  <span v-else-if="item.category_id == 6">游戏讨论</span>
                </div>
              </div>
            </div>
            <div class="guide-stats">
              <span class="guide-stat">{{ item.view_count }} 浏览</span>
              <span class="guide-stat">128 收藏</span>
              <span class="guide-stat">56 评论</span>
            </div>
          </div>
          <router-link :to="`/guides/article/${item.post_id}`" class="guide-title">
            {{ item.title }}
          </router-link>
          <p class="guide-excerpt">
            {{ item.content }}
          </p>
          <div class="guide-tags">
            <span class="guide-tag" v-if="item.category_id == 1">新手指导</span>
            <span class="guide-tag" v-else-if="item.category_id == 2">农场经营</span>
            <span class="guide-tag" v-else-if="item.category_id == 3">矿洞探险</span>
            <span class="guide-tag" v-else-if="item.category_id == 4">社交攻略</span>
            <span class="guide-tag" v-else-if="item.category_id == 5">钓鱼技巧</span>
            <span class="guide-tag" v-else-if="item.category_id == 6">游戏讨论</span>
          </div>
        </div>
      </div>
    </div>

    <!-- 分页 -->
    <div class="pagination">
      <ul class="pagination-list">
        <li class="pagination-item">
          <a href="#" class="pagination-link">
            <span>上一页</span>
          </a>
        </li>
        <li class="pagination-item">
          <a href="#" class="pagination-link">1</a>
        </li>
        <li class="pagination-item">
          <a href="#" class="pagination-link active">2</a>
        </li>
        <li class="pagination-item">
          <a href="#" class="pagination-link">3</a>
        </li>
        <li class="pagination-item">
          <a href="#" class="pagination-link">
            <span>下一页</span>
          </a>
        </li>
      </ul>
    </div>
  </div>
</template>

<style scoped>
body {
  font-family: "Noto Sans SC", sans-serif;
  background-color: #f9fafb;
  margin: 0;
  padding: 0;
}

.container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 0 1rem;
}

/* 导航栏样式 */
.navbar {
  background-color: white;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.navbar-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: 4rem;
  padding: 0 1rem;
}

.navbar-brand {
  display: flex;
  align-items: center;
}

.navbar-logo {
  height: 2rem;
  width: auto;
}

.navbar-title {
  margin-left: 0.5rem;
  font-size: 1.25rem;
  font-weight: bold;
  color: #059669;
}

.navbar-nav {
  display: none;
}

.nav-link {
  color: #6b7280;
  text-decoration: none;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  font-weight: 500;
  border-bottom: 2px solid transparent;
}

.nav-link:hover {
  color: #374151;
  border-bottom-color: #d1d5db;
}

.nav-link.active {
  color: #111827;
  border-bottom-color: #059669;
}

.login-btn {
  background-color: #059669;
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
  font-size: 0.875rem;
  font-weight: 500;
  border: none;
  cursor: pointer;
}

.login-btn:hover {
  background-color: #047857;
}

/* 主要内容区域 */
.main-content {
  padding: 2rem 0;
}

/* 搜索和分类 */
.search-card {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  padding: 1.5rem;
  margin-bottom: 2rem;
}

.search-content {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

@media (min-width: 768px) {
  .search-content {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
  }
}

.search-box {
  flex: 1;
  max-width: 32rem;
}

.search-input {
  width: 100%;
  padding: 0.5rem;
  border: 1px solid #d1d5db;
  border-radius: 0.375rem;
  font-size: 0.875rem;
}

.search-input:focus {
  outline: none;
  border-color: #059669;
  box-shadow: 0 0 0 2px rgba(5, 150, 105, 0.1);
}

.relative {
  display: flex;
  align-items: center;
}

.search-button {
  display: flex;
  height: 100%;
  margin-left: 0.625rem;
  padding: 0.4rem;
  background-color: #059669;
  border: 0;
  border-radius: 0.3125rem;
  cursor: pointer;
}

.search-button:hover {
  background-color: #047857;
}

.search-icon {
  width: 1.25rem;
  height: 1.25rem;
  color: #9ca3af;
}

.filter-group {
  display: flex;
  gap: 1rem;
}

.filter-select {
  padding: 0.5rem;
  border: 1px solid #d1d5db;
  border-radius: 0.375rem;
  font-size: 0.875rem;
  background-color: white;
}

.filter-select:focus {
  outline: none;
  border-color: #059669;
  box-shadow: 0 0 0 2px rgba(5, 150, 105, 0.1);
}

/* 热门标签 */
.tags-card {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  padding: 1.5rem;
  margin-bottom: 2rem;
}

.tags-title {
  font-size: 1.125rem;
  font-weight: 500;
  color: #111827;
  margin-bottom: 1rem;
}

.tags-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.75rem;
  border-radius: 9999px;
  font-size: 0.875rem;
  font-weight: 500;
  background-color: #dcfce7;
  color: #166534;
  text-decoration: none;
}

.tag:hover {
  background-color: #bbf7d0;
}

/* 攻略列表 */
.guide-list {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.guide-card {
  background-color: white;
  border-radius: 0.5rem;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  overflow: hidden;
}

.guide-content {
  padding: 1.5rem;
}

.guide-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.guide-author {
  display: flex;
  align-items: center;
}

.author-avatar {
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 9999px;
}

.author-info {
  margin-left: 0.75rem;
}

.author-name {
  font-size: 0.875rem;
  font-weight: 500;
  color: #111827;
  margin: 0;
}

.author-meta {
  display: flex;
  gap: 0.25rem;
  font-size: 0.875rem;
  color: #6b7280;
}

.guide-stats {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.guide-stat {
  font-size: 0.875rem;
  color: #6b7280;
}

.guide-title {
  display: block;
  margin-top: 1rem;
  font-size: 1.25rem;
  font-weight: 600;
  color: #111827;
  text-decoration: none;
}

.guide-excerpt {
  margin-top: 0.5rem;
  font-size: 0.875rem;
  color: #6b7280;
}

.guide-tags {
  margin-top: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.guide-tag {
  display: inline-flex;
  align-items: center;
  padding: 0.25rem 0.625rem;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 500;
  background-color: #dcfce7;
  color: #166534;
}

/* 分页 */
.pagination {
  margin-top: 2rem;
  display: flex;
  justify-content: center;
}

.pagination-list {
  display: flex;
  list-style: none;
  padding: 0;
  margin: 0;
}

.pagination-item {
  margin: 0 -1px;
}

.pagination-link {
  display: inline-flex;
  align-items: center;
  padding: 0.5rem 1rem;
  border: 1px solid #d1d5db;
  background-color: white;
  color: #6b7280;
  font-size: 0.875rem;
  font-weight: 500;
  text-decoration: none;
}

.pagination-link:hover {
  background-color: #f9fafb;
}

.pagination-link.active {
  background-color: #ecfdf5;
  color: #059669;
}

.empty-tips {
  text-align: center;
  padding: 40px 0;
  color: #888;
}

.empty-icon {
  width: 100px;
  height: 100px;
  margin-bottom: 20px;
  opacity: 0.6;
}

/* 页脚 */
.footer {
  background-color: white;
  margin-top: 3rem;
  padding: 3rem 0;
}

.footer-nav {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 1.25rem;
}

.footer-link {
  color: #6b7280;
  text-decoration: none;
  font-size: 1rem;
  padding: 0.5rem;
}

.footer-link:hover {
  color: #374151;
}

.footer-copyright {
  margin-top: 2rem;
  text-align: center;
  color: #9ca3af;
  font-size: 1rem;
}

/* 响应式设计 */
@media (min-width: 640px) {
  .navbar-nav {
    display: flex;
    gap: 2rem;
  }
}
</style>
